// include header files
var crypto = require('crypto');
var message = require('../lib/message');
var database = require('../lib/database');
var fotoString = require('../lib/fotostring');
var codeOperation = require('../lib/code');

// api create fotola
exports.createFotola = function(request, response) {
	try {
		var result, code;
		
		if (!request.param(fotoString.PARAM)) {
			code = codeOperation.OPERATION_ERROR;
			result = message.MESSAGE_UNEXPECTED_ERROR;
			response.json({"code" : code, "result": result} );
			database.closeDatabase();
	
			return;
		}
		
		var param = JSON.parse(request.param(fotoString.PARAM));
		
		fotolaDatabase.collection(fotoString.FOTO_DB_FOTOLA_TABLE, {
			strict : true
		}, function(databaseError, fotolaCollection) {
			if (databaseError) {
				code = codeOperation.OPERATION_ERROR;
				result = message.MESSAGE_DB_ERROR;
				response.json({"code" : code, "result": result} );
				database.closeDatabase();
				return;
			}
			
			fotolaDatabase.collection(fotoString.FOTO_DB_ACCOUNT_TABLE, {
				strict : true
			}, function(databaseError, fotoAccountCollection) {
				if (databaseError) {
					code = codeOperation.OPERATION_ERROR;
					result = message.MESSAGE_DB_ERROR;
					response.json({"code" : code, "result": result} );
					database.closeDatabase();
					return;
				}
			
				if(!param.tokenKey || !param.name || !param.mode || !param.download || !param.upload)
				{
					code = codeOperation.PARAMETER_ERROR;

					if(!param.tokenKey){
						result = fotoString.TOKEN_KEY_MESSAGE + message.MESSAGE_FIELD_NULL_ERROR;
					}else if(!param.name){
						result = fotoString.NAME + message.MESSAGE_FIELD_NULL_ERROR;
					}else if(!param.mode){
						result = fotoString.MODE + message.MESSAGE_FIELD_NULL_ERROR;
					}else if(!param.download){
						result = fotoString.DOWNLOAD + message.MESSAGE_FIELD_NULL_ERROR;
					}else if(!param.upload){
						result = fotoString.UPLOAD + message.MESSAGE_FIELD_NULL_ERROR;
					}
					
					response.json({"code" : code, "result": result} );
					database.closeDatabase();
					return;
				 }
				
				  fotoAccountCollection.findOne(
						  {tokenKey : param.tokenKey }, 
						  {tokenKey : fotoString.TRUE, email : fotoString.TRUE, _id : fotoString.FALSE}
				  ,function(findAccountError, findAccountResult) {
					
					if (!findAccountResult) {
						code = codeOperation.OPERATION_ERROR;
						result = message.MESSAGE_UNEXPECTED_ERROR;
						response.json({"code" : code, "result": result} );
						database.closeDatabase();
						return;
					}else{
						fotolaCollection.findOne({name : param.name }, {name : fotoString.TRUE, _id : fotoString.FALSE}
						  ,function(findError, findFotolaResult) {
							  if(findFotolaResult){
								  code = codeOperation.OPERATION_ERROR;
									result = message.MESSAGE_FOTOLA_EXIST_ERROR;
									response.json({"code" : code, "result": result} );
									database.closeDatabase();
									return;
							  }
							  
							  param.email = findAccountResult.email;
							  param.cinderallaTime = 24;
							  param.photos = fotoString.EMPTY_STRING;
							  param.createTime = new Date();
							  param.tokenKeyFotola = crypto.createHash(fotoString.MD5).update(
										new Date().getTime().toString()).digest(fotoString.HEX);
							  delete(param.tokenKey);
							
							fotolaCollection.insert(param, {
								safe : true
							}, function(insertObjectError, insertObjectResult) {
								if(!insertObjectError){
									code = codeOperation.OPERATION_SUCCESS;
									result = {"tokenKeyFotola" : param.tokenKeyFotola};
								}else{
									code = codeOperation.OPERATION_ERROR;
									result = message.MESSAGE_UNEXPECTED_ERROR;
								}
								response.json({"code" : code, "result": result} );
								database.closeDatabase();
								return;
							});
					    });						
					}
				});
			});
		});
	} catch (unexpectedError) {
		code = codeOperation.OPERATION_ERROR;
		result = message.MESSAGE_UNEXPECTED_ERROR;
		response.json({"code" : code, "result": result} );
		database.closeDatabase();
		return;
	}
}

// api update fotola
exports.updateFotola = function(request, response) {
	try {
		var result, code;
		
		if (!request.param(fotoString.PARAM)) {
			code = codeOperation.OPERATION_ERROR;
			result = message.MESSAGE_UNEXPECTED_ERROR;
			response.json({"code" : code, "result": result} );
			database.closeDatabase();
	
			return;
		}
		  
		var param = JSON.parse(request.param(fotoString.PARAM));
		
		fotolaDatabase.collection(fotoString.FOTO_DB_FOTOLA_TABLE, {
			strict : true
		}, function(databaseError, fotolaCollection) {
			if (databaseError) {
				code = codeOperation.OPERATION_ERROR;
				result = message.MESSAGE_DB_ERROR;
				response.json({"code" : code, "result": result} );
				database.closeDatabase();
				return;
			}
			
			fotolaDatabase.collection(fotoString.FOTO_DB_ACCOUNT_TABLE, {
				strict : true
			}, function(databaseError, fotoAccountCollection) {
				if (databaseError) {
					code = codeOperation.OPERATION_ERROR;
					result = message.MESSAGE_DB_ERROR;
					response.json({"code" : code, "result": result} );
					database.closeDatabase();
					return;
				}
			
				if(!param.tokenKey || !param.tokenKeyFotola || !param.mode || !param.download || !param.upload)
				{
					code = codeOperation.PARAMETER_ERROR;
					if(!param.tokenKey){
						result = fotoString.TOKEN_KEY_MESSAGE + message.MESSAGE_FIELD_NULL_ERROR;
					}else if(!param.mode){
						result = fotoString.MODE + message.MESSAGE_FIELD_NULL_ERROR;
					}else if(!param.download){
						result = fotoString.DOWNLOAD + message.MESSAGE_FIELD_NULL_ERROR;
					}else if(!param.upload){
						result = fotoString.UPLOAD + message.MESSAGE_FIELD_NULL_ERROR;
					}
					
					response.json({"code" : code, "result": result} );
					database.closeDatabase();
					return;
				 }
				
				  fotoAccountCollection.findOne({ tokenKey : param.tokenKey }, {tokenKey : fotoString.TRUE, _id : fotoString.FALSE}
				  ,function(findAccountError, findAccountResult) {
					if (!findAccountResult) {
						code = codeOperation.OPERATION_ERROR;
						result = message.MESSAGE_UNEXPECTED_ERROR;
						response.json({"code" : code, "result": result} );
						database.closeDatabase();
						return;
					}else{
						fotolaCollection.findOne(
								{ tokenKeyFotola : param.tokenKeyFotola }, 
								{tokenKeyFotola : fotoString.TRUE, _id : fotoString.FALSE}
						  ,function(findError, findFotolaResult) {
							  if(!findFotolaResult){
									code = codeOperation.OPERATION_ERROR;
									result = message.MESSAGE_UNEXPECTED_ERROR;
									response.json({"code" : code, "result": result} );
									database.closeDatabase();
									return;
							  }
							  											
							  fotolaCollection.update({tokenKeyFotola : param.tokenKeyFotola}, 
							       {
									 $set : {
										"download" : param.download,
										"upload" : param.upload,
										"mode" : param.mode
									 }
						  		}, function( updateError, updateObjectResult){
						  			if(updateError){
						  				code = codeOperation.OPERATION_ERROR;
						  				result = message.MESSAGE_UNEXPECTED_ERROR;
						  			}else{
						  				code = codeOperation.OPERATION_SUCCESS;
						  				result = fotoString.EMPTY_STRING;
						  			}
					  				response.json({"code" : code, "result": result} );
					  				database.closeDatabase();
					  				return;
								});
						  });
					   }
				  });
			});
		});
		
	} catch (unexpectedError) {
		code = codeOperation.OPERATION_ERROR;
		result = message.MESSAGE_UNEXPECTED_ERROR;
		response.json({"code" : code, "result": result} );
		database.closeDatabase();
		return;
	}
}

// api delete fotola
exports.deleteFotola = function(request, response) {
	try {
		var result, code;
		if (!request.param(fotoString.PARAM)) {
			code = codeOperation.OPERATION_ERROR;
			result = message.MESSAGE_UNEXPECTED_ERROR;
			response.json({"code" : code, "result": result} );
			database.closeDatabase();
			return;
		}
		
		var param = JSON.parse(request.param(fotoString.PARAM));
	
		fotolaDatabase.collection(fotoString.FOTO_DB_FOTOLA_TABLE, {
			strict : true
		}, function(databaseError, fotolaCollection) {
			if (databaseError) {
				code = codeOperation.OPERATION_ERROR;
				result = message.MESSAGE_DB_ERROR;
				response.json({"code" : code, "result": result} );
				database.closeDatabase();
				return;
			}
		
			if(!param.tokenKey || !param.tokenKeyFotola)
			{
				code = codeOperation.PARAMETER_ERROR;
				
				if(!param.tokenKey){
					result = fotoString.TOKEN_KEY_MESSAGE + message.MESSAGE_FIELD_NULL_ERROR;
				}else if(!param.tokenKeyFotola){
					result = fotoString.TOKEN_KEY_FOTOLA_MESSAGE + message.MESSAGE_FIELD_NULL_ERROR;
				}
	
				response.json({"code" : code, "result" : result} );
				database.closeDatabase();
				return;
			}
			
			fotolaDatabase.collection(fotoString.FOTO_DB_ACCOUNT_TABLE, {
				strict : true
			}, function(databaseError, fotoAccountCollection) {
					if (databaseError) {
						code = codeOperation.OPERATION_ERROR;
						result = message.MESSAGE_DB_ERROR;
						response.json({"code" : code, "result": result} );
						database.closeDatabase();
						return;
					}
					
					fotoAccountCollection.findOne(
							{ tokenKey : param.tokenKey }, 
							{tokenKey : fotoString.TRUE, _id : fotoString.FALSE}
					,function(findObjectError, findObjectResult) {
						if (!findObjectResult) {
							code = codeOperation.OPERATION_ERROR;
							result = message.MESSAGE_UNEXPECTED_ERROR;
							response.json({"code" : code, "result": result} );
							database.closeDatabase();
							return;
						}else{
							fotolaCollection.findOne(
									{tokenKeyFotola : param.tokenKeyFotola}, 
									{tokenKeyFotola : fotoString.TRUE}
							, function(findError, findFotolaResult) {

								if(findFotolaResult){
									fotolaCollection.remove(
											{ tokenKeyFotola :  param.tokenKeyFotola }, fotoString.TRUE, 
									function(removeObjectError, removeObjectResult){
										if(!removeObjectError){
											code = codeOperation.OPERATION_SUCCESS;
											result = fotoString.EMPTY_STRING;								
										}else{
											code = codeOperation.OPERATION_ERROR;
											result = message.MESSAGE_UNEXPECTED_ERROR;
										}
										response.json({"code" : code, "result": result} );
										database.closeDatabase();
										return;
									});
								}else{
									code = codeOperation.OPERATION_ERROR;
									result = message.MESSAGE_UNEXPECTED_ERROR;
									response.json({"code" : code, "result": result} );
									database.closeDatabase();
									return;
								}
							});
						}
					});
			});
		});			
	} catch (unexpectedError) {
		code = codeOperation.OPERATION_ERROR;
		result = message.MESSAGE_UNEXPECTED_ERROR;
		response.json({"code" : code, "result": result} );
		database.closeDatabase();
		return;
	}
}

// instant fotola api
exports.instantFotola = function(request, response) { 
	
}

// list fotola api
exports.listFotola = function(request, response) { 
	try {
		var result, code;
		if (!request.param(fotoString.PARAM)) {
			code = codeOperation.OPERATION_ERROR;
			result = message.MESSAGE_UNEXPECTED_ERROR;
			response.json({"code" : code, "result": result} );
			database.closeDatabase();
			return;
		}
		
		var param = JSON.parse(request.param(fotoString.PARAM));
	
		fotolaDatabase.collection(fotoString.FOTO_DB_FOTOLA_TABLE, {
			strict : true
		}, function(databaseError, fotolaCollection) {
			if (databaseError) {
				code = codeOperation.OPERATION_ERROR;
				result = message.MESSAGE_DB_ERROR;
				response.json({"code" : code, "result": result} );
				database.closeDatabase();
				return;
			}
		
			if(!param.tokenKey)
			{
				code = codeOperation.PARAMETER_ERROR;
				
				if(!param.tokenKey){
					result = fotoString.TOKEN_KEY_MESSAGE + message.MESSAGE_FIELD_NULL_ERROR;
				}
				
				response.json({"code" : code, "result" : result} );
				database.closeDatabase();
				return;
			}
			
			fotolaDatabase.collection(fotoString.FOTO_DB_ACCOUNT_TABLE, {
				strict : true
			}, function(databaseError, fotoAccountCollection) {
					if (databaseError) {
						code = codeOperation.OPERATION_ERROR;
						result = message.MESSAGE_DB_ERROR;
						response.json({"code" : code, "result": result} );
						database.closeDatabase();
						return;
					}
					
					fotoAccountCollection.findOne(
							{ tokenKey : param.tokenKey }, 
							{tokenKey : fotoString.TRUE, _id : fotoString.FALSE}
					,function(findObjectError, findObjectResult) {
						if (!findObjectResult) {
							code = codeOperation.OPERATION_ERROR;
							result = message.MESSAGE_UNEXPECTED_ERROR;
							response.json({"code" : code, "result": result} );
							database.closeDatabase();
							return;
						}else{
							
							fotolaCollection.find().toArray(function(findError, cursor) {
								if(cursor){
									code = codeOperation.OPERATION_SUCCESS;
									result = {"listFotola" : cursor};
								}else{
									code = codeOperation.OPERATION_ERROR;
									response.json({"code" : code, "result": result} );
								}
								response.json({"code" : code, "result": result} );
								database.closeDatabase();
								return;
							});
						}
					});
				});
			});
						
	} catch (unexpectedError) {
		code = codeOperation.OPERATION_ERROR;
		result = message.MESSAGE_UNEXPECTED_ERROR;
		response.json({"code" : code, "result": result} );
		database.closeDatabase();
		return;
	}
}

exports.listFotolaOfAccount = function(request, response) { 
	try {
		var result, code;
		if (!request.param(fotoString.PARAM)) {
			code = codeOperation.OPERATION_ERROR;
			result = message.MESSAGE_UNEXPECTED_ERROR;
			response.json({"code" : code, "result": result} );
			database.closeDatabase();
			return;
		}
		
		var param = JSON.parse(request.param(fotoString.PARAM));
	
		fotolaDatabase.collection(fotoString.FOTO_DB_FOTOLA_TABLE, {
			strict : true
		}, function(databaseError, fotolaCollection) {
			if (databaseError) {
				code = codeOperation.OPERATION_ERROR;
				result = message.MESSAGE_DB_ERROR;
				response.json({"code" : code, "result": result} );
				database.closeDatabase();
				return;
			}
		
			if(!param.tokenKey)
			{
				code = codeOperation.PARAMETER_ERROR;
				
				if(!param.tokenKey){
					result = fotoString.TOKEN_KEY_MESSAGE + message.MESSAGE_FIELD_NULL_ERROR;
				}
				
				response.json({"code" : code, "result" : result} );
				database.closeDatabase();
				return;
			}
			
			fotolaDatabase.collection(fotoString.FOTO_DB_ACCOUNT_TABLE, {
				strict : true
			}, function(databaseError, fotoAccountCollection) {
					if (databaseError) {
						code = codeOperation.OPERATION_ERROR;
						result = message.MESSAGE_DB_ERROR;
						response.json({"code" : code, "result": result} );
						database.closeDatabase();
						return;
					}
					
					fotoAccountCollection.findOne(
							{ tokenKey : param.tokenKey }, 
							{tokenKey : fotoString.TRUE, email: fotoString.TRUE, _id : fotoString.FALSE}
					,function(findObjectError, findObjectResult) {
						if (!findObjectResult) {
							code = codeOperation.OPERATION_ERROR;
							result = message.MESSAGE_UNEXPECTED_ERROR;
							response.json({"code" : code, "result": result} );
							database.closeDatabase();
							return;
						}else{
							
							fotolaCollection.find({email :  findObjectResult.email})
							.toArray(function(findError, cursor) {
								if(cursor){
									code = codeOperation.OPERATION_SUCCESS;
									result = {"listFotolaOfAccount" : cursor};
								}else{
									code = codeOperation.OPERATION_ERROR;
									result = message.MESSAGE_UNEXPECTED_ERROR;
								}
								response.json(code, result);
								database.closeDatabase();
								return;
							});
						}
				});
			});
		});
	
	} catch (unexpectedError) {
		code = codeOperation.OPERATION_ERROR;
		result = message.MESSAGE_UNEXPECTED_ERROR;
		response.json({"code" : code, "result": result} );
		database.closeDatabase();
		return;
	}
}

